package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuiliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utis.ConstantPropertiesUtils;
import com.atguigu.vod.utis.InifVodClient;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
//@CrossOrigin
public class VodController {

    @Autowired
    VodService vodService;

    //上传视频到阿里云
    @PostMapping("/uploadAliyunVod")
    public R uploadAliyunVod(MultipartFile file) throws IOException {
        String id=vodService.uploadAliyunVod(file);
        return R.ok().data("id",id);
    }

    //删除阿里云里的视频
    @DeleteMapping("/deleteAliyunVod/{id}")
    public R deleteAliyunVod(@PathVariable String id){
        try {
            DefaultAcsClient client = InifVodClient.initVodClient(ConstantPropertiesUtils.KEY_ID,ConstantPropertiesUtils.KEY_SECRET);
            //创建删除视频的reauest对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request中设置id
            request.setVideoIds(id);
            client.getAcsResponse(request);
        }catch (Exception e){
            e.printStackTrace();
            throw  new GuiliException(20001,"删除失败！！！");
        }
        return R.ok();
    }

    //删除多个阿里云里的视频
    @DeleteMapping("/deleteDuoAliyunVod")
    public R deleteDuoAliyunVod(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removerDuoVideo(videoIdList);
        return R.ok();
    }

    //根据视频id获取视频凭证
    @GetMapping("/getPlayAuth/{vidoeId}")
    public R getPlayAuth(@PathVariable String vidoeId) throws ClientException {
        String auth=vodService.getPlayAuth(vidoeId);
        System.out.println(auth);
        return R.ok().data("auth",auth);
    }

}
